package cn.sz.hy.ui.widget.top;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;

import cn.sz.hy.event.top.FileOpenEvent;
import cn.sz.hy.event.top.FixHeaderEvent;
import cn.sz.hy.handler.top.FileOpenHandler;
import cn.sz.hy.handler.top.FixHeaderHandler;

public class FileIOWidget extends BasicPanel{

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Text text;
	private Button button;
	private Label label;
	private int type;
	private Button button_1;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public FileIOWidget(Composite parent, int style, int type) {
		super(parent, style);
		this.type = type;
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				toolkit.dispose();
			}
		});
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		setLayout(new GridLayout(4, false));
		
		label = new Label(this, SWT.NONE);
		GridData gd_label = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_label.widthHint = 93;
		label.setLayoutData(gd_label);
		toolkit.adapt(label, true, true);
		label.setText("New Label");
		
		text = new Text(this, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		toolkit.adapt(text, true, true);
		
		button = new Button(this, SWT.NONE);
		GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 80;
		
		button.setLayoutData(gd_button);
		toolkit.adapt(button, true, true);
		button.setText("New Button");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				super.mouseDown(e);
				eBus.execute(new FileOpenEvent(button, FileIOWidget.this.type));
			}
		});
		
		eBus.addComponent(button.hashCode(), text);
		eBus.addHandler(button, new FileOpenHandler());
		
		button_1 = new Button(this, SWT.NONE);
		toolkit.adapt(button_1, true, true);
		button_1.setText("修正表头");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				super.mouseDown(e);
				eBus.execute(new FixHeaderEvent(button_1, FileIOWidget.this.type));
			}
		});
		eBus.addHandler(button_1, new FixHeaderHandler());

	}

	public void setButtonName(String text) {
		this.button.setText(text);
	}

	public void setLabelName(String text) {
		this.label.setText(text);
	}

	public Text getText() {
		return text;
	}
	

}
